# -*- coding:utf-8 -*-
import getopt
import os
import platform
import pysvn
import sys
import shutil

WORKSPACE_PATH = os.path.dirname(os.path.abspath(__file__))
WORKSPACE_COOCAASDK_PATH = '%s/CoocaaSDK' % WORKSPACE_PATH
WORKSPACE_COOCAASDKCONFIG_PATH = '%s/CoocaaSDKConfig' % WORKSPACE_PATH
my_platform = platform.system().lower()


def get_svn_url(path):
    try:
        log("get svn url %s" % path)
        client = pysvn.Client()
        entry = client.info(path)
        return entry.url
    except Exception as e:
        log(e)
        return ""


def svn_update(path):
    log("update svn %s" % path)
    client = pysvn.Client()
    client.cleanup(path)
    client.update(path)
    entry = client.info(path)
    number = entry.commit_revision.number
    return number


def svn_commit_file(file, comment):
    log("commit svn %s(%s)" % (file, comment))
    client = pysvn.Client()

    _comment = 'SuperX Team AutoBuild Tools(STAT,一个依靠写代码为生的群体) commit svn\n%s' % comment
    client.checkin([file], _comment)


def svn_checkout(url, path):
    log("checkout svn from %s to %s" % (url, path))
    client = pysvn.Client()
    client.checkout(url, path)


def log(str):
    print("<<<%s>>> " % str)


def build():
    log("now start setup coocaasdk")
    file_object = open('%s/ccsdk.config' % WORKSPACE_COOCAASDKCONFIG_PATH, 'r')
    lines = file_object.readlines()
    log(lines)
    file_object.close()
    for line in lines:
        kv = line.split('=')
        path = "%s/%s" % (WORKSPACE_COOCAASDK_PATH, kv[0])
        log('check out %s from %s to %s' % (kv[0], kv[1], path))
        _url = get_svn_url(path)
        log('exist svn_url:%s   current %s' % (_url, kv[1]))
        if _url.strip() == kv[1].strip():
            svn_update(path)
        else:
            if 'linux' in my_platform:
                os.system("rm -r %s" % path)
            elif 'windows' in my_platform:
                os.system("rd /s /q %s" % path)
            svn_checkout(kv[1].strip(), path)
    if 'linux' in my_platform:
        os.system("cp -r CoocaaSDKConfig/. ./CoocaaSDK")
    elif 'windows' in my_platform:
        os.system("xcopy CoocaaSDKConfig CoocaaSDK /s /e /y")
    log("now complete checkout coocaasdk")


APPIUMTEST_PROJECT = '%s/AppiumTest' % WORKSPACE_PATH
APPIUM_REPORT = '%s/build/reports/tests' % APPIUMTEST_PROJECT


def test(device_ip, app, apk_file, report_out):
    # log('start test %s:%s@%s' % (app, apk_file, device_ip))
    if 'Hall' in app:
        os.system("adb disconnect")
        os.system("adb connect %s" % device_ip)
        os.system("adb install -r %s" % apk_file)
        # os.system('appium &')
        os.chdir(APPIUMTEST_PROJECT)
        os.system("gradle -q test")
        os.chdir(WORKSPACE_PATH)
        shutil.copytree(APPIUM_REPORT, report_out)
        return APPIUM_REPORT


if __name__ == '__main__':
    reload(sys)
    sys.setdefaultencoding('utf8')

    opts, args = getopt.getopt(sys.argv[1:], 's', ['test=', 'build='])
    for o, a in opts:
        print('%s:%s' % (o, a))
        if 'test' in o:
            bp = a.split(',')
            device_ip = bp[0]
            app = bp[1]
            apk_file = bp[2]
            report_out = bp[3]
            print(test(device_ip, app, apk_file, report_out))
        elif 'build' in o:
            build()
    sys.exit(0)
